package com.mars.admin.modules.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.mars.admin.framework.common.Result;
import com.mars.admin.modules.goods.entity.GoodsCategory;
import com.mars.admin.modules.goods.service.GoodsCategoryService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类控制器
 *
 * @author Mars
 * @version 1.0
 * @date 2025-01-07
 */
@Slf4j
@RestController
@RequestMapping("/api/goods/category")
@Tag(name = "商品分类管理", description = "商品分类相关接口")
@AllArgsConstructor
public class GoodsCategoryController {

    private final GoodsCategoryService goodsCategoryService;

    /**
     * 获取分类树
     */
    @GetMapping("/tree")
    @Operation(summary = "获取分类树", description = "获取所有分类的树形结构")
    public Result<List<GoodsCategory>> getCategoryTree() {
        List<GoodsCategory> categoryTree = goodsCategoryService.getCategoryTree();
        return Result.success(categoryTree);
    }

    /**
     * 根据父级ID获取子分类
     */
    @GetMapping("/children/{parentId}")
    @Operation(summary = "获取子分类", description = "根据父级ID获取子分类列表")
    @Parameter(name = "parentId", description = "父级分类ID", required = true)
    public Result<List<GoodsCategory>> getChildrenByParentId(@PathVariable Long parentId) {
        List<GoodsCategory> children = goodsCategoryService.getChildrenByParentId(parentId);
        return Result.success(children);
    }

    /**
     * 分页查询分类
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询分类", description = "分页查询商品分类列表")
    @SaCheckLogin
    public Result<Page<GoodsCategory>> page(
            @RequestParam(defaultValue = "1") Integer pageNumber,
            @RequestParam(defaultValue = "10") Integer pageSize,
            GoodsCategory category) {
        Page<GoodsCategory> page = Page.of(pageNumber, pageSize);
        Page<GoodsCategory> result = goodsCategoryService.page(page);
        return Result.success(result);
    }

    /**
     * 根据ID查询分类详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询分类详情", description = "根据ID查询商品分类详情")
    @Parameter(name = "id", description = "分类ID", required = true)
    @SaCheckLogin
    public Result<GoodsCategory> getById(@PathVariable Long id) {
        GoodsCategory category = goodsCategoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 新增分类
     */
    @PostMapping
    @Operation(summary = "新增分类", description = "新增商品分类")
    @SaCheckLogin
    public Result<Void> save(@RequestBody GoodsCategory category) {
        // 检查分类编码是否存在
        if (goodsCategoryService.existsCategoryCode(category.getCategoryCode(), null)) {
            return Result.error("分类编码已存在");
        }

        boolean result = goodsCategoryService.save(category);
        return result ? Result.success() : Result.error("保存失败");
    }

    /**
     * 更新分类
     */
    @PutMapping
    @Operation(summary = "更新分类", description = "更新商品分类信息")
    @SaCheckLogin
    public Result<Void> update(@RequestBody GoodsCategory category) {
        // 检查分类编码是否存在
        if (goodsCategoryService.existsCategoryCode(category.getCategoryCode(), category.getId())) {
            return Result.error("分类编码已存在");
        }

        boolean result = goodsCategoryService.updateById(category);
        return result ? Result.success() : Result.error("更新失败");
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类", description = "根据ID删除商品分类")
    @Parameter(name = "id", description = "分类ID", required = true)
    @SaCheckLogin
    public Result<Void> delete(@PathVariable Long id) {
        boolean result = goodsCategoryService.removeById(id);
        return result ? Result.success() : Result.error("删除失败");
    }
}
